package com.jcfk.eam.controller.bd;

import cn.hutool.core.util.StrUtil;
import com.jcfk.common.business.enums.AvailableStatus;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.business.utils.DataBindHelper;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.ReqPageParam;
import com.jcfk.common.web.domain.RetPage;
import com.jcfk.common.web.domain.RetResult;

import com.jcfk.eam.domain.dto.base.EamCustomDTO;
import com.jcfk.eam.domain.dto.base.EamCustomValueDTO;
import com.jcfk.eam.domain.query.base.EamCustomQuery;
import com.jcfk.eam.service.base.EamCustomService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;


import java.util.List;

/**
 * 自定义字段
 *
 * @author awei
 * @email 522891237@gqq.com
 * @date 2024-08-15
 */
@Api(tags = {"自定义字段管理"})
@RestController
@RequestMapping("eamcustom")
public class EamCustomController {
    @Autowired
    private EamCustomService eamCustomService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<EamCustomQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询自定义字段")
    @PostMapping("/queryByPage")
    public RetPage<EamCustomDTO> queryByPage(@RequestBody ReqPageParam<EamCustomQuery> requestParam) {
        DataPage<EamCustomDTO> page = new DataPage<EamCustomDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
                EamCustomDTO.class);
        page = eamCustomService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamCustomQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取自定义字段列表", notes = "获取自定义字段列表", httpMethod = "POST")
    @PostMapping("getDetails")
    public RetResult<List<EamCustomValueDTO>> getDetails(@RequestBody EamCustomQuery query) {

        List<EamCustomValueDTO> list = eamCustomService.getDetails(query);

        return RetResult.ok(list);
    }

    /**
     * 获取信息列表
     *
     * @param query
     * @return
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "EamCustomQuery", name = "query", value = "", required = true)
    })
    @ApiOperation(value = "获取自定义字段列表", notes = "获取自定义字段列表", httpMethod = "POST")
    @PostMapping("getList")
    public RetResult<List<EamCustomDTO>> getList(@RequestBody EamCustomQuery query) {

        List<EamCustomDTO> list = eamCustomService.getList(query);

        return RetResult.ok(list);
    }


    /**
     * 自定义字段
     * 根据id 查询详情
     */
    @ApiOperation("查询自定义字段")
    @GetMapping("/get/{id}")
    public RetResult<EamCustomDTO> get(@PathVariable("id") String id) {
        EamCustomDTO item = eamCustomService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理自定义字段
     */
    @ApiOperation("保存自定义字段")
    @PostMapping("/save")
    @RequestLogHeader(modular = "自定义字段管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid EamCustomDTO eamCustom) {
        // 数据校验
        RetResult ret = eamCustomService.isValid(eamCustom);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(eamCustom.getId())) {
            // 新增
            eamCustomService.insert(eamCustom);
        } else {
            // 更新
            eamCustomService.update(eamCustom);
        }
        return RetResult.ok();

    }


    /**
     * 批量删除自定义字段
     */
    @ApiOperation("批量删除自定义字段")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "自定义字段管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody EamCustomDTO eamCustom) {
        return RetResult.ok(eamCustomService.logicDeleteList(eamCustom));
    }


    /**
     * 删除自定义字段
     */
    @ApiOperation("删除自定义字段")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "自定义字段管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        eamCustomService.deleteItem(id);
        return RetResult.ok();
    }


    /**
     * 删除自定义字段
     */
    @ApiOperation("根据设备类型获取自定义字段")
    @PostMapping("/getConfiguration")
    @RequestLogHeader(modular = "自定义字段管理", businessType = "删除")
    public RetResult<?> getConfiguration(@RequestBody EamCustomQuery dto) {
        List<EamCustomDTO> list = eamCustomService.getConfiguration(dto);
        return RetResult.ok(list);
    }
}
